package com.hzgj.bcl.stats;

import lombok.Getter;
import lombok.Setter;

/**
 * Created by Administrator on 2016/8/18.
 */
public class Metric {
    @Getter
    MethodCounter methodCounter = new MethodCounter();
    @Getter
    TimeCounter timeCounter = null;
    @Getter
    @Setter
    private long target;

    public Metric(long target, TimeCounter timeCounter) {
        this.timeCounter = timeCounter;
        this.target = target;
        timeCounter.setMetric(this);
    }

    public Metric(TimeCounter timeCounter) {
        this(0, timeCounter);
    }

    public boolean hasData() {
        return timeCounter.count() > 0 || methodCounter.hasCount();
    }

    public void update(String value, long time) {
        String[] vv = value.split("_");
        if (vv != null) {
            if (vv.length == 3) {
                timeCounter.update(Long.parseLong(vv[0]), time);
                if ("1".equals(vv[1])) {
                    methodCounter.incSuccess();
                }
                methodCounter.incTotal();
            } else if (vv.length == 1 && value.equals(StatsConst.STATS_TICK_TYPE)) {
                updateTick(time);
            }
        }
    }

    public void updateTick(long time) {
        timeCounter.update(Integer.MAX_VALUE, time);
    }

    public void update(long value, long time, boolean success) {
        timeCounter.update(value, time);
        if (success) {
            methodCounter.incSuccess();
        }
        methodCounter.incTotal();
    }

    public void clean() {
        methodCounter.clean();
        timeCounter.clean();
    }
}
